<?php

ini_set('display_errors',1);
ini_set('display_startup_errors',1);
error_reporting(-1);

require_once  __DIR__ . '/includes/authenticate.php';

require_once __DIR__ . '/includes/BasketbalFunctions.php';

include_once __DIR__ . '/db_bb_connect.php';

include __DIR__ . '/mainpage.php';

$basketbalfunctions = new BasketbalFunctions();

?>

<style type="text/css">

.table a
{
    display:block;
    text-decoration:none;
    color:dimgrey;
}


</style>

<div class="container-fluid">
	
	<div class="row">
		<div class="col-md-2"></div>
		<div class="col-md-8">
			<div class="text-center">
			<h1 class="text-center">Article Archive </h1>

			<p>      
        <a class="btn btn-default btn-sm" href="./article_add.php">Add New Article</a>
        </p>
    </div>

  <table class="table table-hover" id="sorttable">
    <thead>
      <tr>
        <th>Title</th>
        <th>Edit</th>
        <th>Vis.</th>
        <th>Del.</th>
      </tr>
    </thead>
    <tbody>

<?php 

$sql="SELECT ID, title FROM articles;";

$result=$db->query($sql);
 
while($row=$result->fetch_assoc()){

	$article_ID=$row['ID']; 
	$article_title=$row['title'];

 ?>
<tr ><td colspan="4"></td></tr>

<tr id="tablerow<?= $article_ID?>" >
	<td id="bg<?= $article_ID?>">
		<strong><a href="./article_view.php?ID=<?= $article_ID?>"><?= $article_title ?></a></strong>
		<div class="small text-muted"><strong>Visibility:</strong> 
	<?php 
	$visibility = $basketbalfunctions->ShowArticleVisibilityNamelist($article_ID); 
		
	echo $visibility;							
	if ( $visibility != 'nobody') {
		
		?>
		<script type="text/javascript">
			document.getElementById("bg<?= $article_ID?>").style.backgroundColor = "#dde5d0"; 
		</script>
		<?php
	} else {?>

											<script type="text/javascript">
			document.getElementById("bg<?= $article_ID?>").style.backgroundColor = "#ffe6e6"; 
		</script>
		<?php
		
	}


	?></div>
	</td>
	<td>			
		<form action="./article_edit.php" method="post"> <input type="hidden" name="articleID" value="<?= $article_ID ?>" > <button class="btn btn-success btn-sm" type="submit" ><span class="glyphicon glyphicon-pencil" aria-hidden="true" ></span></button></form>
	</td>
	<td>
		<form action="./article_visibility.php" method="post"> <input type="hidden" name="articleID" value="<?= $article_ID ?>" ><button class="btn btn-info btn-sm" type="submit" ><span class="glyphicon glyphicon-eye-open" aria-hidden="true" ></span></button></form>
	</td>
	<td>	 
		<form> <input type="hidden" name="articleID" value="<?= $article_ID ?>"><button class="btn btn-danger btn-sm delete_article" type="submit" ><span class="glyphicon glyphicon-remove-circle" aria-hidden="true" ></span></button></form>
	</td>
</tr>



<?php } ?>


	</tbody>
</table>

			<br><br><br>
			</div>
		<div class="col-md-2"></div>
	</div>

</div>

<script type="text/javascript">

	$(".delete_article").on("click", function(e) {
		postData = $(this.form).serialize();
		var ID = $(this.form).find('input[name="articleID"]').val();

		if( confirm("Are you sure to delete this item?")){
		    $.ajax({
		      type: "POST",
		        url: "./includes/handlers/article_delete_handler.php",
		        data: postData,
		        success: function(data) {    
		          $("#tablerow" + ID).html(data);
		        },
		        error: function (xhr, status, error) {
		          $("#tablerow" + ID).html(xhr.responseText);
		        }
		    });
		 } 
		e.preventDefault();		
	});

	$(document).ready(function() {
    $('#sorttable').dataTable({
    	"pagingType": "full_numbers"
    });
} );
</script>